/*
存放接口 */
import request from '@/utils/request'
import store from '../store'
// 批量导入并导出
export * from './module/Login'
//  封装接口并暴露，方便调用
export const allChannelListAPI = () => request({
  url: '/v1_0/channels'
})

// export const loginApi = ({ mobile, code }) => request({
//   url: '/v1_0/authorizations',
//   method: 'POST',
//   data: {
//     mobile,
//     code
//   }
// })

// 后端在请求头上添加 Access-Control-Allow-Origin   可以解决跨域问题

// 用户频道 导航栏  需要在 请求数据的时候，在请求头上添加用户token
export const getUserChannelApi = () => request({
  url: '/v1_0/user/channels'
  // headers: {
  //   Authorization: 'Bearer ' + store.state.token
  // }
})
// 获取新闻 文章推荐
export const getArticlesListApi = ({ channelid, timestamp }) => request({
  url: '/v1_0/articles',
  method: 'GET',
  params: {
    channel_id: channelid,
    timestamp: timestamp || (new Date()).getTime()
  }

})
// 对文章不感兴趣
export const disLikwArcitleApi = ({ target }) => request({
  url: '/v1_0/article/dislikes',
  method: 'POST',
  data: {
    target
  }
})
// 举报文章
export const articleReportsAPI = ({ type, target, remark }) => request({
  url: '/v1_0/article/reports',
  method: 'POST',
  data: {
    type,
    target,
    remark
  }
})
// 更新文章
export const updateChannelListApi = ({ channels }) => request({
  url: '/v1_0/user/channels',
  method: 'PUT',
  data: {
    channels
  }
})
// 建议联想
export const suggestApi = ({ q }) => request({
  url: '/v1_0/suggestion',
  params: {
    q
  }
})
// 搜索结果
export const searchResultsApi = ({ q, page = 1 }) => request({
  url: '/v1_0/search',
  method: 'GET',
  params: {
    q,
    page
  }
})
// 新闻详情
export const articleDetailApi = ({ id }) => request({
  url: `/v1_0/articles/${id}`
})
// 关注作者
export const followingsApi = ({ target }) => request({
  url: '/v1_0/user/followings',
  method: 'POST',
  data: {
    target
  }
})
// 取消关注
export const unFollowingsApi = ({ target }) => request({
  url: `/v1_0/user/followings/${target}`,
  method: 'DELETE'
})

// 对文章点赞
export const articleLikeApi = ({ target }) => request({
  url: '/v1_0/article/likings',
  method: 'POST',
  data: {
    target
  }
})
// 取消点赞
export const articleDisLikeApi = ({ target }) => request({
  url: `/v1_0/article/likings/${target}`,
  method: 'DELETE'
})

// 获取评论或回复
export const commentListAPI = ({
  type = 'a',
  source,
  offset,
  limit
}) => request({
  url: '/v1_0/comments',
  params: {
    type,
    source,
    offset,
    limit
  }
})
// 评论 - 喜欢
export const commentLikingAPI = ({ comId }) => {
  return request({
    url: '/v1_0/comment/likings',
    method: 'POST',
    data: {
      target: comId
    }
  })
}
// 评论-取消喜欢
export const commentDisLikingAPI = ({ comId }) => {
  return request({
    url: `/v1_0/comment/likings/${comId}`,
    method: 'DELETE'
  })
}
// 发布评论
export const sendCommentAPI = ({ target, content }) => request({
  url: '/v1_0/comments',
  method: 'POST',
  data: {
    target,
    content
  }
})
// 获取用户自己信息
export const getUserInfoApi = () => request({
  url: '/v1_0/user'
})
// 获取用户个人资料
export const getProfileApi = () => request({
  url: '/v1_0/user/profile'
})
// 编辑用户图片资料
export const updatePhotoApi = (photo) => request({
  url: '/v1_0/user/photo',
  method: 'PATCH',
  // 因为传入的是FormData格式的数据，是一个FormData对象，所以不需要结构
  // 该数据所需的请求头与之前的不一样，但是浏览器会自动修改
  data: photo
})
// 编辑用户个人资料
export const updateFielApi = ({ birthday, userName }) => request({
  url: '/v1_0/user/profile',
  method: 'PATCH',
  data: {
    birthday,
    userName
  }
})

// 刷新用户token
export const refrechTokenApi = () => request({
  url: '/v1_0/authorizations',
  method: 'PUT',
  headers: {
    Authorization: 'Bearer ' + store.state.refresh_token
  }
})
